#include "../include/ProtocolParser.h"
#include "../include/TcpConnection.h"
#include "../include/KeyRecommander.h"
#include "../include/WebPageSearcher.h"
#include <iostream>
#include <sstream>

Message ProtocolParser::parse(const std::string &data)
{
    // 直接使用Message的deserialize方法
    return Message::deserialize(data);
}

std::string ProtocolParser::buildResponse(int tag, const std::string &value)
{
    Message response;
    response.tag = tag;
    response.length = value.size();
    response.value = value;

    // 使用Message的serialize方法
    return response.serialize();
}

std::vector<std::string> ProtocolParser::splitPackets(const std::string &data)
{
    std::vector<std::string> packets;
    size_t pos = 0;

    while (pos < data.size())
    {
        size_t packet_len = 0;

        // 使用Message的isCompletePacket方法检查包完整性
        if (Message::isCompletePacket(data.substr(pos), packet_len))
        {
            // 完整的包
            packets.push_back(data.substr(pos, packet_len));
            pos += packet_len;
        }
        else
        {
            break; // 数据不完整
        }
    }

    return packets;
}

void ProtocolParser::handleKeywordRecommendation(const TcpConnectionPtr &conn, const std::string &keyword)
{
    std::cout << "🔍 处理关键字推荐请求: '" << keyword << "'" << std::endl;

    KeyRecommander recommander;
    std::vector<std::string> suggestions;
    recommander.doQuery(keyword, suggestions);

    // 构建JSON响应
    std::string response_data = "{\"type\":\"suggestion\",\"keyword\":\"" + keyword + "\",\"suggestions\":[";
    for (size_t i = 0; i < suggestions.size(); ++i)
    {
        response_data += "\"" + suggestions[i] + "\"";
        if (i < suggestions.size() - 1)
        {
            response_data += ",";
        }
    }
    response_data += "]}";

    std::cout << "📤 准备返回 " << suggestions.size() << " 个推荐" << std::endl;

    // 构建并发送响应
    std::string tlv_response = buildResponse(1, response_data);
    conn->send(tlv_response);

    std::cout << "✅ 关键字推荐响应发送完成" << std::endl;
}

void ProtocolParser::handleWebSearch(const TcpConnectionPtr &conn, const std::string &query)
{
    std::cout << "🔍 处理网页搜索请求: '" << query << "'" << std::endl;

    WebPageSearcher searcher;
    std::vector<WebPageResult> results;
    searcher.doQuery(query, results);

    // 构建JSON响应
    std::stringstream json;
    json << "{\"type\":\"web_search\",\"keyword\":\"" << query << "\",\"results\":[";

    for (size_t i = 0; i < results.size(); ++i)
    {
        const auto &result = results[i];
        json << "{";
        json << "\"id\":" << result.doc_id << ",";
        json << "\"title\":\"" << result.title << "\",";
        json << "\"link\":\"" << result.link << "\",";
        json << "\"abstract\":\"" << result.abstract << "\",";
        json << "\"score\":" << result.score;
        json << "}";

        if (i < results.size() - 1)
        {
            json << ",";
        }
    }

    json << "]}";

    std::string response_data = json.str();

    // 构建并发送响应
    std::string tlv_response = buildResponse(2, response_data);
    conn->send(tlv_response);

    std::cout << "✅ 网页搜索结果发送完成: " << results.size() << " 个结果" << std::endl;
}